﻿/*    
    This file is part of d-mixr.

    d-mixr is free software: you can redistribute it and/or modify
    it under the terms of the GNU General Public License as published by
    the Free Software Foundation, either version 3 of the License, or
    (at your option) any later version.

    d-mixr is distributed in the hope that it will be useful,
    but WITHOUT ANY WARRANTY; without even the implied warranty of
    MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
    GNU General Public License for more details.

    You should have received a copy of the GNU General Public License
    along with d-mixr.  If not, see <http://www.gnu.org/licenses/>.
*/

using Dmixr.Model;
using System;
using System.Windows.Input;
using System.Xml.Serialization;

namespace Dmixr.ViewModel
{
    public class ChannelStripViewModel : ViewModelBase
    {
        public ChannelStrip strip;
       
        public delegate void RequestDeleteHandler(ChannelStripViewModel strip);
        public event RequestDeleteHandler RequestDeleteEvent;
        RelayCommand _destroyChannelStrip;

        /*  For xml serializer*/
        public ChannelStripViewModel()
        {
        }

        public ChannelStripViewModel(ChannelStrip c)
        {
            strip = c;
        }

        public void Init()
        {
            strip.NewVolumeEvent += this.NewVolumeHandler;
            strip.ConnectionStateEvent += this.ConnectionStateHandler;
        }

        public void Term()
        {
            strip.NewVolumeEvent -= this.NewVolumeHandler;
            strip.ConnectionStateEvent -= this.ConnectionStateHandler;
        }

        [XmlIgnoreAttribute]
        public String AppName
        {
            get { return strip.Name; }
            set {
                strip.Name = value;
                OnPropertyChanged("AppName");
            }
        }

        public bool IsConnected
        {
            get
            {
                return strip.Session != null;
            }
        }

        public bool IsSessionActive
        {
            get
            {
                if (strip.Session == null)
                    return false;
                return strip.Session.State == CoreAudioApi.AudioSessionState.AudioSessionStateActive;
            }
        }

        public int MuteMidiControlChange
        {
            get
            {
                return strip.MuteMidiBinding.ControlChange;
            }
            set
            {
                strip.MuteMidiBinding.ControlChange = value;
            }
        }

        public int MuteMidiChannel
        {
            get
            {
                return strip.MuteMidiBinding.Channel;
            }
            set
            {
                strip.MuteMidiBinding.Channel = value;
            }
        }
        [XmlIgnoreAttribute]
        public double Volume
        {
            get { return AudioHelpers.LinearToDb(strip.Volume); }
            set {
                strip.Volume = (float)AudioHelpers.DbToLinear(value);
                OnPropertyChanged("Volume");
            }
        }

        public int VolumeMidiControlChange
        {
            get
            {
                return strip.VolumeMidiBinding.ControlChange;
            }
            set
            {
                strip.VolumeMidiBinding.ControlChange = value;
            }
        }

        public int VolumeMidiChannel
        {
            get
            {
                return strip.VolumeMidiBinding.Channel;
            }
            set
            {
                strip.VolumeMidiBinding.Channel = value;
            }
        }

        [XmlIgnoreAttribute]
        public bool IsMuted
        {
            get
            {
                return strip.IsMuted;
            }

            set
            {
                strip.IsMuted = value;
                OnPropertyChanged("IsMuted");
            }
        }

        public bool TryReconnect()
        {
            return strip.ConnectToAudioSession();
        }

        void NewVolumeHandler(float NewVolume, bool newMute)
        {
            OnPropertyChanged("Volume");
            OnPropertyChanged("IsMuted");
        }

        void ConnectionStateHandler(bool isConnected)
        {
            OnPropertyChanged("IsConnected");
        }

        public ICommand DestroyChannelStripCommand
        {
            get
            {
                if (_destroyChannelStrip == null)
                    _destroyChannelStrip = new RelayCommand(OnDestroyChannelStrip);
                return _destroyChannelStrip;
            }
        }

        void OnDestroyChannelStrip(object toDelete)
        {
            if (RequestDeleteEvent != null)
                RequestDeleteEvent(this);
        }
    }
}
